package cn.flying.system.service.cm.impl;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.flying.base.common.vo.PageRequest;
import cn.flying.base.common.vo.PageResponse;
import cn.flying.base.common.vo.XmSelect;
import cn.flying.system.common.enums.SeqTypeEnum;
import cn.flying.system.domain.cm.CmPosition;
import cn.flying.system.domain.cm.CmPositionExample;
import cn.flying.system.domain.cm.CmPositionUser;
import cn.flying.system.domain.cm.CmPositionUserExample;
import cn.flying.system.mapper.cm.CmPositionMapper;
import cn.flying.system.mapper.cm.CmPositionUserMapper;
import cn.flying.system.service.cm.CmPositionService;
import cn.flying.system.service.cm.CmSeqService;

/**
 * @description: 岗位信息接口实现类
 * @author: lvyong
 * @date: 2022年06月05日 20:17
 * @version: 1.0
 */
@Service
public class CmPositionServiceImpl implements CmPositionService {

    @Resource
    private CmPositionMapper cmPositionMapper;
    @Resource
    private CmPositionUserMapper cmPositionUserMapper;
    @Autowired
    private CmSeqService cmSeqService;

    /**
     * 查询列表-分页
     *
     * @param pageRequest
     * @return
     */
    @Override
    public PageResponse<CmPosition> getPositionPageList(PageRequest<CmPosition> pageRequest) {
        Integer pageNum = pageRequest.getPageNum();
        Integer pageSize = pageRequest.getPageSize();
        CmPosition cmPosition = pageRequest.getParam();
        CmPositionExample example = new CmPositionExample();
        CmPositionExample.Criteria criteria = example.createCriteria();
        if (StringUtils.hasText(cmPosition.getPostId())) {
            criteria.andPostIdEqualTo(cmPosition.getPostId());
        }
        if (StringUtils.hasText(cmPosition.getPostName())) {
            criteria.andPostNameLike("%" + cmPosition.getPostName() + "%");
        }
        PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        Page<CmPosition> page = (Page<CmPosition>) cmPositionMapper.selectByExample(example);
        return new PageResponse(page.getTotal(), pageNum, page.getResult());
    }

    /**
     * 获取岗位下拉框数据 xm-select
     *
     * @return
     */
    @Override
    public List<XmSelect> getPositionList() {
        CmPositionExample example = new CmPositionExample();
        List<CmPosition> positions = cmPositionMapper.selectByExample(example);
        List<XmSelect> list = new ArrayList<>();
        XmSelect xmSelect;
        for (CmPosition position : positions) {
            xmSelect = new XmSelect();
            xmSelect.setName(position.getPostName());
            xmSelect.setValue(position.getPostId());
            xmSelect.setSelected(false);
            xmSelect.setDisabled(false);

            list.add(xmSelect);
        }
        return list;
    }

    /**
     * 根据主键ID查询信息
     *
     * @param id
     * @return
     */
    @Override
    public CmPosition getPositionById(Long id) {
        return cmPositionMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询用户的岗位
     *
     * @param userId
     * @return
     */
    @Override
    public List<CmPositionUser> getPositionUserList(String userId) {
        CmPositionUserExample example = new CmPositionUserExample();
        CmPositionUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        return cmPositionUserMapper.selectByExample(example);
    }

    /**
     * 保存新增信息
     *
     * @param position
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertPosition(CmPosition position) {
        String seq = cmSeqService.getSeq(SeqTypeEnum.POSITION.name());
        position.setPostId(seq);
        cmPositionMapper.insertSelective(position);
    }

    /**
     * 保存修改信息
     *
     * @param position
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updatePositionById(CmPosition position) {
        cmPositionMapper.updateByPrimaryKeySelective(position);
    }

    /**
     * 根据主键ID删除
     *
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deletePositionById(Long id) {
        cmPositionMapper.deleteByPrimaryKey(id);
    }
}
